package org.intelligentsia.keystone.mygod;

/**
 * 
 * @author jguibert
 */
public class StringUtils {

	public static int filterInt(final String src) {
		try {
			return Integer.parseInt(StringUtils.filterString(src, "1234567890"));
		} catch (final NumberFormatException e) {
			return 0;
		}
	}

	public static long filterLong(final String src) {
		try {
			return Long.parseLong(StringUtils.filterString(src, "1234567890"));
		} catch (final NumberFormatException e) {
			return 0;
		}
	}

	public static String filterString(final String str, final String filter) {
		if ((str == null) || (filter == null)) {
			return "";
		}

		final byte[] org = str.getBytes();
		final byte[] mask = filter.getBytes();
		final byte[] ret = new byte[org.length];
		int count = 0;
		int i;
		for (i = 0; i < org.length; i++) {
			final byte letter = org[i];
			for (final byte element : mask) {
				if (letter == element) {
					ret[count] = letter;
					count++;
					break;
				}
			}
		}
		return new String(ret).trim();
	}
}
